home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wiconsetter.lzh / wIdentify / wIdentify.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  13KB  |  529 lines

  1. /*
  2.  *  WIDENTIFY   A companion utility to wIconSetter and wIconify that allows
  3.  *              you to determine the commands necessary to properly identify
  4.  *              a window or screen in the wIconSetter initialization file.
  5.  *
  6.  *  wIdentify.c     main code for wIdentify.
  7.  *
  8.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  9.  *  You may use this code, provided this copyright notice is kept intact.
  10.  */
  11.  
  12. #include "wIdentify.h"
  13. #include <devices/input.h>
  14.  
  15. static char *program   = PROGRAM;
  16. static char *copyright = COPYRIGHT;
  17.  
  18.  
  19. struct MsgPort *IdPort;         /* Port for messages in when NEW is specified */
  20. static char *WindowName;        /* Name of window to identify */
  21. static char *ScreenName;        /* Name of screen holding window */
  22.  
  23. /*  Storage for SetFunction return values */
  24.  
  25. long OldOpenWindow;
  26. long OldSetWindowTitles;
  27. long OldOpenScreen;
  28.  
  29. #define TOUPPER(c)          (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)
  30. #define MATCH(n1,n2)        (PrefixMatch(n1,n2) == 0)
  31.  
  32.  
  33. #define ARGMATCH(s)     (stricmp(argv[0],s) == 0)
  34. #define FUNCTION(s)     (ARGMATCH(s) && function == JUST_EXIT)
  35. #define NAME(s)\
  36.    (ARGMATCH(s) && (function == JUST_EXIT || function == IDENTIFY_ONE))
  37.  
  38. #define PARAMETER(f,a,s)\
  39. {\
  40.    if (argc >= 2)\
  41.    {\
  42.       if (function == JUST_EXIT) function = f;\
  43.       a = argv[1];\
  44.       argc--; argv++;\
  45.    } else {\
  46.       printf(s);\
  47.       function = SHOW_USAGE;\
  48.    }\
  49. }
  50.  
  51.  
  52. /*
  53.  *  DoExit()
  54.  *
  55.  *  General purpose error-exit routine.  Print an error message if one was
  56.  *  supplied (it can have up to three parameters), and then clean up any
  57.  *  memory, libraries, etc. that need to be handled before exiting.
  58.  */
  59.  
  60. void DoExit(s,x1,x2,x3)
  61. char *s, *x1, *x2, *x3;
  62. {
  63.    long status = EXIT_OK;
  64.    
  65.    if (s != NULL)
  66.    {
  67.       printf(s,x1,x2,x3);
  68.       printf("\n");
  69.       status = EXIT_ERROR;
  70.    }
  71.    if (IdPort)          DeletePort(IdPort);
  72.    if (IntuitionBase)   CloseLibrary(IntuitionBase);
  73.    exit(status);
  74. }
  75.  
  76.  
  77. /*
  78.  *  CheckLibOpen()
  79.  *
  80.  *  Call OpenLibrary() for the specified library, and check that the 
  81.  *  open succeeded.
  82.  */
  83.  
  84. static void CheckLibOpen(lib,name,rev)
  85. APTR *lib;
  86. char *name;
  87. int rev;
  88. {
  89.    extern APTR OpenLibrary();
  90.  
  91.    if ((*lib = OpenLibrary(name,(LONG)rev)) == NULL)
  92.       DoExit("Can't open %s",name);
  93. }
  94.  
  95.  
  96. /*
  97.  *  SetVectors()
  98.  *
  99.  *  Set the Intuition library vectors for the routines we are trapping
  100.  *  to the routines specified by the handler.  Save the old routine pointers
  101.  *  for later replacement.
  102.  */
  103.  
  104. static void SetVectors()
  105. {
  106.    OldOpenWindow = SetFunction(IntuitionBase,&LVOOpenWindow,&aOpenWindow);
  107.    OldSetWindowTitles =
  108.       SetFunction(IntuitionBase,&LVOSetWindowTitles,&aSetWindowTitles);
  109.    OldOpenScreen = SetFunction(IntuitionBase,&LVOOpenScreen,&aOpenScreen);
  110. }
  111.  
  112.  
  113. /*
  114.  *  UnSetVectors()
  115.  *
  116.  *  Replace the old Intuition library vectors, but make sure that no one
  117.  *  else has changed them behind our back.  If they are not the same as
  118.  *  what we set them to originally, then put back the ones that we found,
  119.  *  and return an error status.
  120.  */
  121.  
  122. static int UnSetVectors()
  123. {
  124.    long NewOpenWindow;
  125.    long NewSetWindowTitles;
  126.    long NewOpenScreen;
  127.    int status = FALSE;
  128.  
  129.    NewOpenWindow = SetFunction(IntuitionBase,&LVOOpenWindow,OldOpenWindow);
  130.    NewSetWindowTitles =
  131.       SetFunction(IntuitionBase,&LVOSetWindowTitles,OldSetWindowTitles);
  132.    NewOpenScreen = SetFunction(IntuitionBase,&LVOOpenScreen,OldOpenScreen);
  133.    if (NewOpenWindow      != (long) &aOpenWindow ||
  134.        NewSetWindowTitles != (long) &aSetWindowTitles ||
  135.        NewOpenScreen      != (long) &aOpenScreen)
  136.    {
  137.       SetFunction(IntuitionBase,&LVOOpenWindow,NewOpenWindow);
  138.       SetFunction(IntuitionBase,&LVOSetWindowTitles,NewSetWindowTitles);
  139.       SetFunction(IntuitionBase,&LVOOpenScreen,NewOpenScreen);
  140.       status = TRUE;
  141.    }
  142.    return(status);
  143. }
  144.  
  145.  
  146. /*
  147.  *  ParseArguments()
  148.  *
  149.  *  Initialize the wIdentify function to just exiting.
  150.  *  If we were started from the WB, just exit.
  151.  *  If we did not have any parameters, identify active window.
  152.  *  While there are more arguments to consider,
  153.  *    Get the next argument
  154.  *    If it is NEW or ALL, set the function appropriately.
  155.  *    If it is WINDOW, get the window name or show an error.
  156.  *    If it is SCREEN, get the screen name or show an error.
  157.  *    Otherwise show un-recognized parameter error and show usage.
  158.  *  Return the function to perform.
  159.  */
  160.  
  161. static int ParseArguments(argc,argv)
  162. int argc;
  163. char **argv;
  164. {
  165.    int function = JUST_EXIT;
  166.    
  167.    if (argc == 0) return(JUST_EXIT);
  168.    if (argc == 1) return(IDENTIFY_ONE);
  169.    while (--argc > 0)
  170.    {
  171.       argv++;
  172.       if (FUNCTION("NEW"))      function = IDENTIFY_NEW;
  173.       else if (FUNCTION("ALL")) function = IDENTIFY_ALL;
  174.       else if (NAME("WINDOW"))
  175.          PARAMETER(IDENTIFY_ONE,WindowName,"Window name required\n")
  176.       else if (NAME("SCREEN"))
  177.          PARAMETER(IDENTIFY_ONE,ScreenName,"Screen name required\n")
  178.       else
  179.       {
  180.          printf("Unrecognized parameter '%s'\n",*argv);
  181.          function = SHOW_USAGE;
  182.       }
  183.    }
  184.    return(function);
  185. }
  186.  
  187. /*
  188.  *  PrefixMatch()
  189.  *
  190.  *  Case-insensitive prefix match.  NULL pointers and empty strings
  191.  *  only match themselves, and are not considered prefixes to any string.
  192.  *
  193.  *  Return <0 if the first is smaller than the second,
  194.  *         =0 if the first is a prefix of the second,
  195.  *         >1 if the first is larger than the second.
  196.  */
  197.  
  198. static int PrefixMatch(s1,s2)
  199. char *s1,*s2;
  200. {
  201.    int match = -1;
  202.  
  203.    if (s1)
  204.    {
  205.       if (s2)
  206.       {
  207.          while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
  208.          if (*s1 == 0) match = 0; else match = *s1 - *s2;
  209.       } else match = 1;
  210.    } else if (s2 == NULL) match = 0;
  211.    return(match);
  212. }
  213.  
  214.  
  215. /*
  216.  *  *FindScreen()
  217.  *
  218.  *  Lock IntuitionBase so nothing happens while we look.
  219.  *  If there is a name to look for,
  220.  *    Start at the first screen, and look for a screen whose title
  221.  *      matches the given name.  If none, return NULL.
  222.  *  Otherwise, use the active screen.
  223.  *  Unlock Intuition.
  224.  *  Return the screen pointer found, if any.
  225.  */
  226.  
  227. static struct Screen *FindScreen(ScreenName)
  228. char *ScreenName;
  229. {
  230.    struct Screen *theScreen;
  231.    long ILock, LockIBase();
  232.  
  233.    ILock = LockIBase(0L);
  234.    if (ScreenName)
  235.    {
  236.       theScreen = IntuitionBase->FirstScreen;
  237.       while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
  238.          theScreen = theScreen->NextScreen;
  239.    } else {
  240.       theScreen = IntuitionBase->ActiveScreen;
  241.    }
  242.    UnlockIBase(ILock);
  243.    return(theScreen);
  244. }
  245.  
  246.  
  247. /*
  248.  *  *FindWindow()
  249.  *
  250.  *  Lock Intuition so nothing happens while we look.
  251.  *  If there is a window name given,
  252.  *    Start with the first window on the screen and look through
  253.  *      the window list for one that matches the given name.
  254.  *      Return NULL if none found.
  255.  *  Otherwise, use the active window.
  256.  *  Unlock Intuition.
  257.  *  Return the window pointer, if any.
  258.  */
  259.  
  260. static struct Window *FindWindow(WindowName,theScreen)
  261. char *WindowName;
  262. struct Screen *theScreen;
  263. {
  264.    struct Window *theWindow;
  265.    long ILock, LockIBase();
  266.  
  267.    ILock = LockIBase(0L);
  268.    if (WindowName)
  269.    {
  270.       theWindow = theScreen->FirstWindow;
  271.       while (theWindow && PrefixMatch(WindowName,theWindow->Title))
  272.          theWindow = theWindow->NextWindow;
  273.    } else {
  274.       theWindow = IntuitionBase->ActiveWindow;
  275.    }
  276.    UnlockIBase(ILock);
  277.    return(theWindow);
  278. }
  279.  
  280.  
  281. /*
  282.  *  DoMessage()
  283.  *
  284.  *  Get the message's screen and window pointers.
  285.  *  If the Message has a program, print its name, otherwise print '[Unknown]'
  286.  *  If the screen has a title, print it, otherwise print '[NULL]'
  287.  *  If this is not a screen-open message
  288.  *    if the window has a title, print it, otherwise print '[NULL]'
  289.  */
  290.  
  291. static void DoMessage(theMessage)
  292. struct IdMessage *theMessage;
  293. {
  294.    struct Window *theWindow = theMessage->Window;
  295.    struct Screen *theScreen = theMessage->Screen;
  296.  
  297.    if (theMessage->Program)
  298.       printf("\nProgram: '%s'\n",theMessage->Program);
  299.      else
  300.       printf("\nProgram: [Unknown]\n");
  301.  
  302.    if (theScreen->DefaultTitle)
  303.       printf("  Screen: '%s'\n",theScreen->DefaultTitle);
  304.      else
  305.       printf("  Screen: [NULL]\n");
  306.  
  307.    if (theWindow != SCREENICON)
  308.    {
  309.       if (theWindow->Title)
  310.          printf("    Window: '%s'\n",theWindow->Title);
  311.         else
  312.          printf("    Window: [NULL]\n");
  313.    }
  314. }
  315.  
  316.  
  317. /*
  318.  *  IdentifyWindow()
  319.  *
  320.  *  Get ther port of the window
  321.  *  If there is a port, and it has a task to signal,
  322.  *    Get the task's name and print it, otherwise print '[ANY]'
  323.  *  If the window's screen has a title, print it, otherwise print '[NULL]'
  324.  *  If the window itself has a title, print it, otherwise print '[NULL]'
  325.  */
  326.  
  327. static void IdentifyWindow(theWindow)
  328. struct Window *theWindow;
  329. {
  330.    struct MsgPort *thePort;
  331.    char Program[MAXNAME];
  332.    
  333.    thePort = theWindow->UserPort;
  334.    if (thePort && thePort->mp_SigTask != NULL &&
  335.       (thePort->mp_Flags & PF_ACTION) == PA_SIGNAL)
  336.           GetProgramName(Program,thePort->mp_SigTask),
  337.           printf("Program: '%s'\n",Program);
  338.          else
  339.           printf("Program: [ANY]\n");
  340.  
  341.    if (theWindow->WScreen->DefaultTitle)
  342.       printf("  Screen: '%s'\n",theWindow->WScreen->DefaultTitle);
  343.      else
  344.       printf("  Screen: [NULL]\n");
  345.  
  346.    if (theWindow->Title)
  347.       printf("    Window: '%s'\n",theWindow->Title);
  348.      else
  349.       printf("    Window: [NULL]\n");
  350. }
  351.  
  352.  
  353. /*
  354.  *  IdentifyScreen()
  355.  *
  356.  *  Get the screen's first window
  357.  *  While there are still windows to consider
  358.  *    Print a blank line then the window's name, etc.
  359.  *    Go on to the next window
  360.  */
  361.  
  362. static void IdentifyScreen(theScreen)
  363. struct Screen *theScreen;
  364. {
  365.    struct Window *theWindow;
  366.    
  367.    Forbid();
  368.    theWindow = theScreen->FirstWindow;
  369.    while (theWindow)
  370.    {
  371.       printf("\n");
  372.       IdentifyWindow(theWindow);
  373.       theWindow = theWindow->NextWindow;
  374.    }
  375.    Permit();
  376. }
  377.  
  378.  
  379. /*
  380.  *  IdentifyNew()
  381.  *
  382.  *  Create a port for messages from the handler
  383.  *  Trap the intuition routines, and give a message that we are running
  384.  *  Get the wait signals
  385.  *  As long as we haven't bee told otherwise,
  386.  *    Wait for a message or CTRL-C
  387.  *    While ther are messages in the port,
  388.  *      Do each message and reply to it
  389.  *    If we were signalled to stop,
  390.  *      Try to remove the trap routines
  391.  *      If unsuccessful, print a message, otherwise, end the loop
  392.  *  Indicate that wIdentify is finished.
  393.  */
  394.  
  395. static void IdentifyNew()
  396. {
  397.    ULONG Signals;
  398.    ULONG WaitSignals;
  399.    int NotDone = TRUE;
  400.    struct IdMessage *theMessage;
  401.  
  402.    IdPort = CreatePort(0,0);
  403.    if (IdPort == NULL) DoExit("Can't Create IdPort");
  404.  
  405.    SetVectors();
  406.    printf("wIdentify Running...\n");
  407.    WaitSignals = (ONE << IdPort->mp_SigBit) | SIGBREAKF_CTRL_C;
  408.  
  409.    while (NotDone)
  410.    {
  411.       Signals = Wait(WaitSignals);
  412.       while (theMessage = GetMsg(IdPort))
  413.       {
  414.          DoMessage(theMessage);
  415.          ReplyMsg(theMessage);
  416.       }
  417.       if (Signals & SIGBREAKF_CTRL_C)
  418.       {
  419.          NotDone = UnSetVectors();
  420.          if (NotDone)
  421.          {
  422.             printf("[vectors have been changed - can't cancel]\n");
  423.             printf("wIdentify Running...\n");
  424.          }
  425.       }
  426.    }
  427.    printf("\nwIdentify Done\n");
  428. }
  429.  
  430.  
  431. /*
  432.  *  IdentifyOne()
  433.  *
  434.  *  Find the specified screen.
  435.  *  If can't find it, error
  436.  *  Otherwise,
  437.  *    If a window was not specified, but a screen was, do the entire screen,
  438.  *    Otherwise
  439.  *      Find the specified window on the screen
  440.  *      If found, identify it, otherwise error
  441.  */
  442.  
  443. static void IdentifyOne()
  444. {
  445.    struct Window *theWindow;
  446.    struct Screen *theScreen;
  447.    
  448.    Forbid();
  449.    theScreen = FindScreen(ScreenName);
  450.    if (theScreen == NULL)
  451.    {
  452.       printf("Can't find screen '%s'\n",ScreenName);
  453.    } else {
  454.       if (WindowName == NULL && ScreenName)
  455.       {
  456.          IdentifyScreen(theScreen);
  457.          printf("\n");
  458.       } else {
  459.          theWindow = FindWindow(WindowName,theScreen);
  460.          if (theWindow) IdentifyWindow(theWindow);
  461.             else printf("Can't find window '%s' on screen '%s'\n",
  462.                             WindowName,theScreen->DefaultTitle);
  463.       }
  464.    }
  465.    Permit();
  466. }
  467.  
  468.  
  469. /*
  470.  *  IdentifyAll()
  471.  *
  472.  *  Get the first Intuition screen
  473.  *  While there are more screens
  474.  *    Identify all windows on the screen and go to the next screen
  475.  */
  476.  
  477. static void IdentifyAll()
  478. {
  479.    struct Screen *theScreen;
  480.    
  481.    Forbid();
  482.    theScreen = IntuitionBase->FirstScreen;
  483.    while (theScreen)
  484.    {
  485.       IdentifyScreen(theScreen);
  486.       theScreen = theScreen->NextScreen;
  487.    }
  488.    Permit();
  489.    printf("\n");
  490. }
  491.  
  492.  
  493. /*
  494.  *  main()
  495.  *
  496.  *  Open the Intuition library
  497.  *  Parse the argument list, and do the proper thing:
  498.  *    Show usage, identify new windows, one window, or all windows
  499.  *  Exit and clean up
  500.  */
  501.  
  502. void main(argc,argv)
  503. int argc;
  504. char **argv;
  505. {
  506.    CheckLibOpen(&IntuitionBase,"intuition.library",INTUITION_REV);
  507.    
  508.    switch(ParseArguments(argc,argv))
  509.    {
  510.       case SHOW_USAGE:
  511.          printf("Usage:  %s\n",USAGE);
  512.          break;
  513.  
  514.       case IDENTIFY_NEW:
  515.          IdentifyNew();
  516.          break;
  517.  
  518.       case IDENTIFY_ONE:
  519.          IdentifyOne();
  520.          break;
  521.  
  522.       case IDENTIFY_ALL:
  523.          IdentifyAll();
  524.          break;
  525.    }
  526.  
  527.    DoExit(NULL);
  528. }
  529.